<?php
/**
 * 中文字符串处理
 */
class Str_Chinese
{
    /**
     * 中文或英文字符串转化成数组
     */
    function str2arr($str)
    {
        $arr = array();
        mb_internal_encoding(SYS_CHARSET);
        $length = mb_strlen($str);
        for ($i=0; $i<$length; $i++)
        {
        	$arr[] = mb_substr($str, $i, 1);
        }
        return $arr;
    }

    /**
     * 汉字繁转简
     */
    function f2j($name)
    {
        static $f2j;
        if (empty($f2j))
        {
            $filename = CORE_DATA_PATH."chinese/f2j.txt";
            $content = file_get_contents($filename);
            $fn_list = explode("\n", $content);
            mb_internal_encoding(SYS_CHARSET);
            foreach($fn_list as $fn)
            {
                $fn = trim($fn);
                if (mb_strlen($fn) != 2)
                {
                    continue;
                }
                $f2j[mb_substr($fn, 0, 1)] = mb_substr($fn, 1, 1);
            }
        }


        $arr = self::str2arr($name);
        $acount = count($arr);
        for ($i=0; $i<$acount; $i++)
        {
            if (array_key_exists($arr[$i], $f2j))
            {
                $arr[$i] = $f2j[$arr[$i]];
            }
        }
        return implode("", $arr);
    }

    function hz2py($str , &$firststr_arr , &$pystr_arr , &$filterstr="")
    {
        $firststr_arr = array("");
        $pystr_arr = array("");
        $filterstr = "";
        $singleflag = 0;

        $id = dba_open(CORE_DATA_PATH."chinese/hz2py","r","gdbm");
        if(!$id)
        {
            return;
        }
        mb_internal_encoding(SYS_CHARSET);
        $len = mb_strlen($str);
        for($i=0; $i<$len; $i++)
        {
            $char = mb_substr($str, $i, 1);
            if(mb_strwidth($char) > 1)
            {
                $pystrtemp = dba_fetch($char, $id);
                $arr = split("," , $pystrtemp);

                //拼音
                $pystr_arr2 = array();
                foreach($pystr_arr as $py)
                {
                    foreach($arr as $value)
                    {
                        $pystr_arr2[] = $py.strtolower($value);
                    }
                }
                $pystr_arr = $pystr_arr2;

                //拼音头字母
                $firststr_arr2 = array();
                foreach($firststr_arr as $firstpy)
                {
                    foreach($arr as $value)
                    {
                        $firststr_arr2[] = $firstpy.substr(strtolower($value) , 0 , 1);
                    }
                }
                $firststr_arr = $firststr_arr2;

                //过滤字符串
                if(0 != strlen($pystrtemp))
                {
                    $filterstr .= $char;
                }
            }
            else
            {
                $char = strtolower($char);
                $ord = ord($char);
                if ($ord >= 97 && $ord <= 122)
                {
                    //拼音
                    foreach($pystr_arr as $k=>$py)
                    {
                        $pystr_arr[$k] .= $char;
                    }
                    //拼音头字母
                    foreach($firststr_arr as $k=>$firstpy)
                    {
                        $firststr_arr[$k] .= $char;
                    }
                    //过滤字符串
                    $filterstr .= $char;
                }
           }
           }
        dba_close($id);
        return true;
    }

	// 是否匹配拼音或者英文
	function isMatchPy($keyword,$str)
	{
		$keyword = strtolower($keyword);
		self::hz2py($str,$first,$all);
		foreach($first as $v)
		{
			$ret1 = strstr($v,$keyword);
		}
		foreach($all as $v)
		{
			$ret2 = strstr($v,$keyword);
		}
		return ( $ret1 || $ret2);
	}

	/**
	 * @param $arr array( 146=>'张三', 161=>'李四', ..)
	 * @return id列表
	 */
	function suggest($arr, $key)
	{
        $res1 = array();
        $res2 = array();
        $res3 = array();
        $res4 = array();

        if(empty($key))
        {
            return array();
        }

        foreach($arr as $id=>$name)
        {
            //1. 完全匹配
            if (0 == strncasecmp($name,$key,strlen($key)))
            {
                $res1[] = $id;
                continue;
            }

            //2. 汉字第一个字母匹配
            $key = strtolower($key);
            Str_Chinese::hz2py($name,$first,$all);
            $found = false;
            foreach($first as $v)
            {
                if ( 0 == strncasecmp($v, $key, strlen($key)) )
                {
                    $res2[] = $id;
                    $found = true;
                    break;
                }
            }
            if ($found) continue;

            //3. 全名搜索
            if (strstr($name, $key))
            {
                $res3[] = $id;
                continue;
            }

            //4. 全拼搜索
            foreach($first as $v)
            {
                $ret = strstr($v,$key);
                if ($ret) break;
            }
            if($ret)
            {
                $res4[] = $id;
                continue;
            }
            foreach($all as $v)
            {
                $ret = strstr($v,$key);
                if ($ret) break;
            }
            if($ret)
            {
                $res4[] = $id;
                continue;
            }
        }

        return array_merge($res1, $res2, $res3, $res4);
	}

    function subString($str, $length, $ext = '')
    {
    	if (function_exists('mb_internal_encoding'))
    	{
	        mb_internal_encoding(SYS_CHARSET);
	        return mb_strimwidth($str, 0, $length, $ext);
	    }
	    else
	    {
	    	return $str;
	    }
    }

    function lenString($srcstr)
	{
		mb_internal_encoding(SYS_CHARSET);
		return mb_strwidth($srcstr);
	}

	function getAstro($month, $day)
	{
		if (($month == 12 && $day >= 22) || ($month == 1 && $day <= 20))
		{
			return "摩羯座";
		}
		else if (($month == 1 && $day >= 21) || ($month == 2 && $day <= 19))
		{
			return "水瓶座";
		}
		else if (($month == 2 && $day >= 20) || ($month == 3 && $day <= 20))
		{
			return "双鱼座";
		}
		else if (($month == 3 && $day >= 21) || ($month == 4 && $day <= 20))
		{
			return "白羊座";
		}
		else if (($month == 4 && $day >= 21) || ($month == 5 && $day <= 21))
		{
			return "金牛座";
		}
		else if (($month == 5 && $day >= 22) || ($month == 6 && $day <= 21))
		{
			return "双子座";
		}
		else if (($month == 6 && $day >= 22) || ($month == 7 && $day <= 22))
		{
			return "巨蟹座";
		}
		else if (($month == 7 && $day >= 23) || ($month == 8 && $day <= 23))
		{
			return "狮子座";
		}
		else if (($month == 8 && $day >= 24) || ($month == 9 && $day <= 23))
		{
			return "处女座";
		}
		else if (($month == 9 && $day >= 24) || ($month == 10 && $day <= 23))
		{
			return "天秤座";
		}
		else if (($month == 10 && $day >= 24) || ($month == 11 && $day <= 22))
		{
			return "天蝎座";
		}
		else if (($month == 11 && $day >= 23) || ($month == 12 && $day <= 21))
		{
			return "射手座";
		}
		return "";
	}

    /**
     * 检查字符串中是否含有标点
     */
    function symbol($str)
    {
        return preg_match('/[^\x{4e00}-\x{9fa5}0-9A-Za-z]/u', $str);
    }

	/**
     * 过滤空格与换行符转换为一个半角空格
	 * 连续多个空格与换行符仅转换为一个空格
	 * 去除头尾空格
     */
	function filterSpaceLine($content, $length = 0, $ext = '')
	{
		$length = intval($length);
		if ($length)
		{
			$content = Str_Chinese::subString($content,$length,$ext);
		}
		$content = trim($content);
		$content = trim($content, "\n");
		$content = str_replace(array("\r\n", "\r", "\n"), "\n", $content);
		$content = preg_replace("/\n/", " ", $content);
		$content = htmlspecialchars($content);
		$content = preg_replace("/\s+/", "&nbsp;", $content);
		return $content;
	}
}
?>